package cn.edu.neusoft.neufooddemo.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.activity.MyCartActivity;
import cn.edu.neusoft.neufooddemo.bean.OrderInfo;
import cn.edu.neusoft.neufooddemo.bean.Result;
import cn.edu.neusoft.neufooddemo.common.Constants;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;

/**
 * Created by Shinelon on 2017/3/31.
 */

public class CartAdapter extends RecyclerViewAdapter<CartAdapter.ViewHolder> implements RetrofitListener<Result>{

    private FoodModel foodModel;
    private OnItemClickListener mOnItemClickListener;


    public CartAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final OrderInfo entity = (OrderInfo) data.get(position);
        Picasso.get().load(Constants.BASE_URL+entity.getPic()).into(holder.mycart_pic);
        holder.mycart_price.setText("单价：￥" +entity.getPrice());
        holder.mycart_foodname.setText(entity.getFoodname());
        holder.mycart_num.setText("数量："+entity.getNum());
        if(entity.getContent()!=null){
            holder.mycart_delete.setVisibility(View.GONE);
        }
        holder.mycart_itemid.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ((OrderInfo) data.get(position)).setChecked(isChecked);
                double sum=0;
                for(int i=0;i<data.size();i++)
                {
                    if(((OrderInfo)data.get(i)).isChecked()) {
                        double p = ((OrderInfo) data.get(i)).getPrice();
                        int n = Integer.parseInt(((OrderInfo) data.get(i)).getNum());
                        sum += p * n;
                    }
                }
                MyCartActivity.cart_sum.setText(sum+"元");
            }
        });
        holder.mycart_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                            new AlertDialog.Builder(context).setTitle("删除")
                                    .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                                        public void onClick(DialogInterface dialog, int which) {
                                            foodModel=new FoodModel();
                                           foodModel.doDeleteItem(entity.getItem_id(),CartAdapter.this);
                                        }
                                    }).setNegativeButton("取消",null)
                                    .show();
            }
        });
        if( mOnItemClickListener!= null&&holder.mycart_itemid.isChecked()){
            holder.mycart_itemid.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(position);
                }
            });
        }
    }

    public List getData() {
        List<OrderInfo> cart=new ArrayList<>();
        for(int i=0;i<data.size();i++)
         if(((OrderInfo)data.get(i)).isChecked())
             cart.add((OrderInfo)data.get(i));
        return cart;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView mycart_price, mycart_foodname, mycart_num, mycart_orderid;
        Button mycart_delete;
        ImageView mycart_pic;
        CheckBox mycart_itemid;
        private List data;

        public ViewHolder(View itemView) {
            super(itemView);
            mycart_orderid = (TextView) itemView.findViewById(R.id.mycart_orderid);
            mycart_itemid = (CheckBox) itemView.findViewById(R.id.mycart_itemid);
            mycart_price = (TextView) itemView.findViewById(R.id.mycart_price);
            mycart_foodname = (TextView) itemView.findViewById(R.id.mycart_foodname);
            mycart_num = (TextView) itemView.findViewById(R.id.mycart_num);
            mycart_delete = (Button) itemView.findViewById(R.id.mycart_delete);
            mycart_pic = (ImageView) itemView.findViewById(R.id.mycart_pic);
        }

    }


    @Override
    public void onSuccess(Result result, int flag) {
        if(result.getSuccess().equals("1")){
            Intent intent=new Intent(context, MyCartActivity.class);
            context.startActivity(intent);
            Toast.makeText(context,"删除成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context,"删除失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail() {
        Toast.makeText(context,"网络错误", Toast.LENGTH_SHORT).show();
    }

    public interface OnItemClickListener{
        void onClick(int position);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this. mOnItemClickListener=onItemClickListener;
    }
}